package com.wing.productionPlan.service;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.JsonResult;
import com.wing.productionPlan.dao.ProfessionDao;
import com.wing.productionPlan.model.entity.ProProfession;
import com.wing.productionPlan.model.request.ProfessionForm;
import com.wing.productionPlan.model.response.ProfessionVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 *  工种-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class ProfessionServiceImpl extends ServiceImpl<ProfessionDao, ProProfession> implements ProfessionService {


    @Override
    public Page<ProfessionVO> list4app(Page<ProfessionVO> pg, Map map) {
        List<ProfessionVO> vos = this.baseMapper.list4app(pg, map);
        pg.setRecords(vos);
        return pg;
    }

    @Override
    public List<ProfessionVO> list4app(Map map) {
        return this.baseMapper.list4app(map);
    }

    @Override
    public JsonResult add(ProfessionForm professionForm) {
        //校验
        ProProfession profession =new ProProfession();
        CopyOptions copyOptions = CopyOptions.create()
                 .setIgnoreError(true)
                .setIgnoreNullValue(true);
        //todo增加重复值校验
        String nameExit = baseMapper.getNameExit(professionForm.getProfessionName());
        if(StringUtils.isNotBlank(nameExit)){
            return JsonResult.fail("工种名称已经存在");
        }
        BeanUtil.copyProperties(professionForm, profession, copyOptions);
        int insert = baseMapper.insert(profession);
        if(insert>0){
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }

    @Override
    public JsonResult edit(ProfessionForm professionForm) {
        //校验
        ProProfession profession =new ProProfession();
        CopyOptions copyOptions = CopyOptions.create()
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        ProProfession profession1 = baseMapper.selectById(professionForm.getId());
        if(profession1==null){
            return JsonResult.fail("修改失败");
        }
        if(profession1.getProfessionName().equals(professionForm.getProfessionName())){
         }else{
            String nameExit = baseMapper.getNameExit(professionForm.getProfessionName());
            if(StringUtils.isNotBlank(nameExit)){
                return JsonResult.fail("工种名称已经存在");
            }
        }
        BeanUtil.copyProperties(professionForm, profession, copyOptions);
        int i = baseMapper.updateById(profession);
        if(i>0){
            return JsonResult.success("修改成功");
        }
        return JsonResult.fail("修改失败");
    }

    @Override
    public int editStatus(ProfessionForm professionForm) {
        ProProfession profession =new ProProfession();
        profession.setId(professionForm.getId());
        profession.setProfessionStatus(professionForm.getProfessionStatus());
        return baseMapper.updateById(profession);
    }

    @Override
    public List<ProProfession> getAllProfession() {
        QueryWrapper<ProProfession> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("profession_status", 1); // 直接使用数据库字段名

        List<ProProfession> resultList = baseMapper.selectList(queryWrapper);

        return resultList;
    }


}

